import pygame
import random


class BaseSprite(pygame.sprite.Sprite):
    def __init__(self, filename, speed=1):
        super().__init__()
        self.image = pygame.image.load(filename)
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self, *args):
        self.rect.y +=self.speed


class BgSprite(BaseSprite):
    """背景图片类"""
    def __init__(self, is_pre=False):
        super().__init__('./images/background.png', 5)
        if is_pre:
            self.rect.y = -700

    def update(self, *args):
        super().update()
        if self.rect.y>=700:
            self.rect.y=-700


class PlaneSprite(BaseSprite):
    """我方飞机类"""
    def __init__(self):
        super().__init__('./images/me1.png')
        self.rect.centerx = 240
        self.rect.y = 550
        self.bullet_group = pygame.sprite.Group()

    def fire(self):
        l_x = self.rect.centerx-self.rect.width//4-8
        l_y = self.rect.centery-20
        r_x = self.rect.centerx+self.rect.width//4+8
        r_y = self.rect.centery-20
        bullet1 = BulletSprite(l_x, l_y)
        bullet2 = BulletSprite(r_x, r_y)
        self.bullet_group.add([bullet1, bullet2])



class BulletSprite(BaseSprite):
    def __init__(self, x, y, speed=1):
        super().__init__('./images/bullet1.png', speed)
        self.rect.x = x
        self.rect.y = y

    def update(self, *args):
        self.rect.y -= self.speed
        if self.rect.bottom <0:
            self.kill()

class EnemySprite(BaseSprite):
    def __init__(self, speed=1):
        super().__init__('./images/enemy1.png', speed)
        x = random.randint(20, 460)
        self.rect.x = x

    def update(self, *args):
        super().update()
        if self.rect.y > 700:
            self.kill()
class PlaneGame:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((480, 700))
        pygame.display.set_caption('飞机大战')
        self.clock = pygame.time.Clock()
        pygame.time.set_timer(pygame.USEREVENT, 500)
        pygame.time.set_timer(20, 500)
        self.is_loop = True
        self.create_sprites()


    def start(self):
        while self.is_loop:
            self.__event_handler()
            self.screen.fill((0,0,0))
            self.update_sprites()
            self.clock.tick(40)
            pygame.display.update()
        pygame.quit()

    def __event_handler(self):
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.is_loop = False
            elif e.type == pygame.USEREVENT:
                self.plane.fire()
            elif e.type == 20:
                enemy = EnemySprite()
                self.enemy_group.add(enemy)

            elif e.type == pygame.KEYDOWN:
                if e.key == pygame.K_LEFT:
                    self.plane.rect.x -= 5
                    if self.plane.rect.x < 0:
                        self.plane.rect.x = 5
                elif e.key == pygame.K_RIGHT:
                    self.plane.rect.x += 5
                    if self.plane.rect.x >480-self.plane.rect.width:
                        self.plane.rect.x = 480-self.plane.rect.width-5



    def create_sprites(self):
        bg1 = BgSprite()
        bg2 = BgSprite(is_pre=True)
        self.bg_group = pygame.sprite.Group(bg1, bg2)
        self.plane = PlaneSprite()
        self.plane_group = pygame.sprite.Group(self.plane)
        self.enemy_group = pygame.sprite.Group()

    def update_sprites(self):
        self.bg_group.update()
        self.bg_group.draw(self.screen)
        self.plane_group.draw(self.screen)
        self.plane.bullet_group.update()
        self.plane.bullet_group.draw(self.screen)
        self.enemy_group.update()
        self.enemy_group.draw(self.screen)

pg = PlaneGame()
pg.start()

